package net.linxuehua.server.controller.admapi;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.linxuehua.commons.core.R;
import net.linxuehua.commons.util.StringUtil;
import net.linxuehua.server.po.Food;
import net.linxuehua.server.po.MenuList;
import net.linxuehua.server.po.Sort;
import net.linxuehua.server.service.FoodService;
import net.linxuehua.server.service.MenuListService;
import net.linxuehua.server.service.SortService;
import net.linxuehua.server.vo.MenuListDetailVO;
import net.linxuehua.server.vo.MenuListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenyan
 * @createTime 2021/1/12 9:42
 * @description description
 */
@Slf4j
@Api(tags = {"菜谱接口"})
@RestController
@RequestMapping(value = "/admapi/menuList")
public class MenuListController {
    @Autowired
    private MenuListService menuListService;
    @Autowired
    private FoodService foodService;
    @Autowired
    private SortService sortService;

    @ApiOperation(value = "获取菜谱信息")
    @GetMapping(value = "/getInfo")
    public R getInfo(@RequestParam(required = false, defaultValue = "") String sort, @RequestParam(required = false, defaultValue = "") String name) {
        QueryWrapper<MenuList> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(sort)) {
            QueryWrapper<Sort> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("name", sort);
            Sort sort1 = sortService.getOne(queryWrapper1);
            if (sort1 == null) {
                return R.error("该分类不存在！");
            }
            queryWrapper.eq("sort_id", sort1.getId());
        }
        if (StringUtil.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }
        List<MenuList> menuListList = menuListService.list(queryWrapper);
        List<MenuListVO> menuListVOList = new ArrayList<>();
        for (MenuList menuList : menuListList) {
            MenuListVO menuListVO = new MenuListVO();
            menuListVO.setId(menuList.getId());
            menuListVO.setDescription(menuList.getDescription());
            menuListVO.setImg(menuList.getImg());
            menuListVO.setName(menuList.getName());
            menuListVO.setPrice(menuList.getPrice() * 0.8);
            menuListVO.setTime(menuList.getTime());
            menuListVOList.add(menuListVO);
        }
        return R.data(menuListVOList);
    }

    @ApiOperation(value = "获取菜谱详细信息")
    @GetMapping(value = "/getInfoDetail")
    public R getInfoDetail(Integer menuListId) {
        if (menuListId == null || menuListId == 0) {
            return R.error("请输入菜谱id");
        }
        MenuList menuList = menuListService.getById(menuListId);
        MenuListDetailVO menuListDetailVO = new MenuListDetailVO();
        menuListDetailVO.setDescription(menuList.getDescription());
        menuListDetailVO.setImg(menuList.getImg());
        menuListDetailVO.setName(menuList.getName());
        menuListDetailVO.setPrice(menuList.getPrice());
        List<Food> foodList = new ArrayList<>();
        String string = menuList.getFoodId();
        String[] str = string.split("\\,");
        for (String s : str) {
            int foodId = Integer.valueOf(s).intValue();
            QueryWrapper<Food> foodQueryWrapper = new QueryWrapper<>();
            foodQueryWrapper.eq("food_id", foodId);
            Food food = foodService.getOne(foodQueryWrapper);
            foodList.add(food);
        }
        menuListDetailVO.setFoodList(foodList);
        return R.data(menuListDetailVO);
    }

}
